package cn.declare.controller;


import cn.declare.bean.Material;
import cn.declare.bean.Materialtype;
import cn.declare.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author：皇甫
 */
@RestController("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;
    /**
     * 查询
     * @param pageIndex
     * @param materialTypeName
     * @return
     */
    @RequestMapping("/getMaterialList")
    public List<Material> getMaterialList(@PathVariable("pageIndex") int pageIndex,
                                        @PathVariable("materialTypeName")String materialTypeName){
        List<Material> list = materialService.getMaterialList(pageIndex, materialTypeName);
        return list;
    }

    /**
     * 增加
     * @param material
     * @return
     */
    @RequestMapping("/addMaterial")
    public String addMaterial(@RequestBody Material material){
        int i=materialService.addMaterial(material);
        if (i>0){
            return "增加成功";
        }else {
            return "增加失败";
        }
    }

    /**
     * 通过id查询对应数据
     * @param materialId
     * @return
     */
    @RequestMapping("/getMaterialId/{materialId}")
    public Material getMaterialId(@PathVariable("materialId")int materialId){
        Material MaterialId1 = materialService.getMaterialId(materialId);
        return MaterialId1;
    }

    /**
     * 修改
     * @param material
     * @return
     */
    @RequestMapping("/updateMaterial")
    public String updateMaterial(@RequestBody Material material){
        Material material1 = materialService.getMaterialId((int) material.getMaterialId());
        if (material1 != null) {
            int i = materialService.updateMaterial(material);
            if (i>0){
                return "修改成功";
            }else {
                return "修改失败";
            }
        }else {
            return "没有修改";
        }
    }

    /**
     * 删除
     * @param materialId
     * @return
     */
    @RequestMapping("/deleteMaterial")
    public String deleteMaterial(@PathVariable("materialId")int materialId){
        int i=materialService.deleteMaterial(materialId);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败！";
        }
    }
//产品类型
    /**
     * 查询所有原材料类型
     * @return
     */
    @RequestMapping("/getMaterialtypeList")
    public List<Materialtype>getMaterialtypeList(){
        List<Materialtype> list = materialService.getMaterialtypeList();
        return list;
    }

}
